#define _CRT_SECURE_NO_WARNINGS 1


//https://leetcode.cn/problems/merge-sorted-array/description/
//
//void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n) {
//    int tmp1 = m - 1;
//    int tmp2 = n - 1;
//    int end = m + n - 1;
//    while (tmp1 >= 0 && tmp2 >= 0)
//    {
//        if (*(nums1 + tmp1) > *(nums2 + tmp2))
//        {
//            *(nums1 + end) = *(nums1 + tmp1);
//            end--;
//            tmp1--;
//        }
//        else
//        {
//            *(nums1 + end) = *(nums2 + tmp2);
//            end--;
//            tmp2--;
//        }
//    }
//    if (tmp1 < 0)
//    {
//        while (tmp2 >= 0)
//        {
//            *(nums1 + end) = *(nums2 + tmp2);
//            end--;
//            tmp2--;
//        }
//    }
//    else
//    {
//        while (tmp1 >= 0)
//        {
//            *(nums1 + end) = *(nums1 + tmp1);
//            end--;
//            tmp1--;
//        }
//    }
//}


#include <stdio.h>
#include <stdlib.h>


typedef struct ListNode {
    int data;
    struct ListNode* next;
}listnode;

struct ListNode* Buysltnode(int n)
{
    struct ListNode* tmp = (struct ListNode*)malloc(sizeof(struct ListNode));
    if (tmp == NULL)
    {
        perror("malloc Buysltnode");
        exit(-1);
    }
    tmp->data = n;
    tmp->next = NULL;
    return tmp;
}

struct ListNode* Creatlist(int n, int arr[20])
{
    struct ListNode* front = NULL;
    struct ListNode* tmp = NULL;
    int i = 0;
    for (i = 0; i < n; i++)
    {
        struct ListNode* new = Buysltnode(arr[i]);
        if (new == NULL)
        {
            perror("malloc Buysltnode");
            exit(-1);
        }
        if (front == NULL)
        {
            front = new;
            tmp = new;
        }
        else
        {
            tmp->next = new;
            tmp = tmp->next;
        }
    }
    return front;
}

void SLTprint(struct ListNode* head)
{
    struct ListNode* tmp = head;
    while (tmp)
    {
        printf("%d ", tmp->data);
        tmp = tmp->next;
    }
    printf("\n");
}


struct ListNode* removeElements(struct ListNode* head, int val) {
    struct ListNode* front = head;
    struct ListNode* tmp = NULL;
    struct ListNode* returndata = NULL;
    while (front)
    {
        if (front->data == val)
        {
            if (front->next == NULL)
            {
                tmp->next = NULL;
                struct ListNode* next = front->next;
                free(front);
                front = next;
            }
            else
            {
                struct ListNode* next = front->next;
                free(front);
                front = next;
            }
            continue;
        }
        if (tmp == NULL)
        {
            tmp = front;
            returndata = front;
            front = front->next;
        }
        else
        {
            tmp->next = front;
            tmp = tmp->next;
            front = front->next;
        }
    }
    return returndata;
}


int main()
{
    int n = 7;
    int arr[] = { 1,2,6,3,4,5,6 };
    struct ListNode* new = Creatlist(n, arr);

    removeElements(new, 6);

    SLTprint(new);
	return 0;
}



//https://leetcode.cn/problems/reverse-linked-list/description/
//
//struct ListNode* reverseList(struct ListNode* head) {
//    struct ListNode* tmp = head;
//    struct ListNode* right = NULL;
//    struct ListNode* left = NULL;
//    while (tmp)
//    {
//        right = tmp->next;
//        tmp->next = left;
//        left = tmp;
//        tmp = right;
//    }
//    return left;
//}